package com.find.answer.handle;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.find.answer.domain.common.BaseContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Slf4j
@Component
public class FindMetaObjectHandler implements MetaObjectHandler {
    /**
     * 插入时候的更新策略
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill object...");
        /**
         * 通过ThreadLocal来把id储存起来。
         */
        Long id = BaseContextHolder.getCurrentId();
        this.setFieldValByName("createTime", LocalDateTime.now(), metaObject);
        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
        this.setFieldValByName("updateUser", id, metaObject);
        this.setFieldValByName("createUser", id, metaObject);
    }

    /**
     * 更新时候的更新策略
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill object...");
        Long id = BaseContextHolder.getCurrentId();
        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
        this.setFieldValByName("updateUser", id, metaObject);
    }
}
